import { refundList, changeOrderInfo, orderDetail, orderSendGoods} from '../services/api';
import { message } from 'antd';

export default {
  namespace: 'refundList',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    loading : false,
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(refundList, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {data : response},
        });
      }
    },
    *changeInfo({ payload }, { call, put }) {
      const response = yield call(changeOrderInfo, payload);
      if(response){
        message.success('成功');
        if(payload.status){
          //修改状态值
          yield put({
            type: 'saveChangeInfo',
            payload: payload,
          });
        }

      }else{
        message.error('失败');
      }
    },
    *getOrderDetail({ payload }, { call, put }) {
      const response = yield call(orderDetail, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {orderDetail : response},
        });
      }
    },
    *sendGoods({ payload }, { call, put }) {
      const response = yield call(orderSendGoods, payload);
      if(response){
        message.success('成功');
        yield put({
          type: 'saveSendGoods',
          payload: payload,
        });
      }else{
        message.error('失败');
      }
    },
  },

  reducers: {
    save(state, { payload }){
      return {
        ...state,
        ...payload
      }
    },
    saveChangeInfo(state, { payload }){
      const { orderId, status } = payload;
      let temp = {...state.data};

      temp.list.forEach(v=>{
        if(v.orderId == orderId){
          v.status = status
        }
      });

      return {
        ...state,
        data : data,
      }
    },
  },
  saveSendGoods(state, { payload }){
    const { orderId } = payload;
    let temp = {...state.data};

    temp.list.forEach(v=>{
      if(v.orderId == orderId){   //修改当前记录为发货状态
        v.status = 103
      }
    });

    return {
      ...state,
      data : data,
    }
  }
};
